package id.co.gomobile.assessment

class Item {

	public static final NO_ANSWER = "No Answer"
	public static final SINGLE_ANSWER = "Single Answer"
	public static final MULTIPLE_ANSWER = "Multiple Answer"

	// associations
	static belongsTo = [test: Test]
    static hasMany = [options: ItemOption]

    static mapping = {
		options sort:'id'
	}

                      
	// properties
	String text
	String category

	Date dateCreated
	Date lastUpdated
		
    static constraints = {
		text(size:0..2000, blank: false)
		category(nullable: true)
    }
	
	def type() {
		int correctAnswer = ItemOption.countByItemAndCorrect(this, true)
		if(correctAnswer == 0) {
			return Item.NO_ANSWER
		}
		else if(correctAnswer == 1) {
			return Item.SINGLE_ANSWER
		}
		else {
			return Item.MULTIPLE_ANSWER
		}
	}

}
